<template>
  <div class="min-h content-layout">
    <div class="bg-wt">
      <!-- 楼层tab导航 -->
      <SwitchBar ref="tabBar" :data="tabData" :route-type="routeType" @change-id="getFloorId"/>
      <!-- 房型列表 -->
      <HouseList :key="floorId" :base-data="roomVoList" :floor-data="tabData" :floorId="floorId"
                 :route-type="routeType"/>
    </div>
  </div>
</template>
<script setup>
import useUserStore from '@/store/modules/user'
import {getRoomBedDeviceList, listAllDeviceFloor} from '@/api/nursing/floor'
import SwitchBar from '@/components/switchBar/switchBartop.vue'
import HouseList from './components/List.vue'

const route = useRoute()
const userStore = useUserStore()
const tabBar = ref(null)
const tabData = ref([])
const roomVoList = ref([])
const floorId = ref('')
const floorIndex = ref(null)
const routeType = ref(null)
const allData = ref([])
const timerId = ref(null)

onUnmounted(() => {
  clearInterval(timerId.value)
})

onMounted(() => {
  if (route.query.type !== undefined) {
    routeType.value = route.query.type
  }
  getAllFloorList()
})

// 获取楼层数据
const getAllFloorList = async () => {
  const res = await listAllDeviceFloor()
  allData.value = res.data
  if (res.code === 200) {
    tabData.value = res.data
    if (floorId.value === '' && tabData.value.length) {
      floorId.value = tabData.value[0].id
      userStore.floorId = tabData.value[0].id
    }
    getList()
  }
}

// 根据不同的楼层id楼层信息
const getList = async () => {
  const res = await getRoomBedDeviceList(floorId.value)
  if (res.code === 200) {
    roomVoList.value = res.data
  }
}

// 获取楼层id
const getFloorId = (id, i) => {
  userStore.floorId = floorId.value
  floorIndex.value = i
  floorId.value = id
  getList()
}

</script>

<style lang="less" src="./../floor/index.scss"></style>
<style lang="scss" scoped>
.smartBed {
  min-height: calc(100vh - 130px);
}
</style>
